﻿using CommonUtils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CommonUtils
{
    public class SendSMSHelper
    {

        public static string SMSApi = System.Configuration.ConfigurationManager.AppSettings["sms_api"];
        public static string SMSUser = System.Configuration.ConfigurationManager.AppSettings["sms_user"];
        public static string SMSPWD = System.Configuration.ConfigurationManager.AppSettings["sms_pwd"];
        public static string SMSSignature = System.Configuration.ConfigurationManager.AppSettings["sms_signature"];
        public static SendSMSResult SendSMS(string Mobile, string Content)
        {
#if DEBUG
            return new SendSMSResult() { Result = "发送成功", MsgID = System.Guid.NewGuid().ToString() };
#endif
            IDictionary<string, string> p = new Dictionary<string, string>();
            p.Add("User", SMSUser);
            p.Add("Pass", SMSPWD);
            p.Add("Mobiles", Mobile);
            p.Add("Content", Content.Contains("【") ? Content : Content + SMSSignature);

            string sr = HTTPRequestUtils.PostForm(SMSApi + "/SMS/SendSMS", p, System.Text.Encoding.UTF8);
            if (sr == "")
            {
                return new SendSMSResult() { Result = "网络错误，读取结果超时" };
            }

            return JsonConvert.DeserializeObject<SendSMSResult>(sr);

        }

        public static GetReportResult GetReport()
        {
            try
            {
                string url = SMSApi + "/SMS/GetReport";
                IDictionary<string, string> p = new Dictionary<string, string>();
                p.Add("User", SMSUser);
                p.Add("Pass", SMSPWD);
                string sr = HTTPRequestUtils.PostForm(url, p, System.Text.Encoding.UTF8);
                if (sr == "")
                {
                    LogHelper.Error("读取状态报告返回结果为空");
                    return null;
                }

                GetReportResult result = JsonConvert.DeserializeObject<GetReportResult>(sr);

                return result;
            }
            catch (Exception ex)
            {
                LogHelper.Error("读取状态报告发生异常", ex);
                return null;
            }
        }
       
    }


    public class SendSMSResult
    {
        public string Result { get; set; }
        public string MsgID { get; set; }
    }
    public class SMSReport
    {
        //短信发送标识
        public string MsgID { get; set; }
        //接收短信的号码
        public string Mobile { get; set; }
        //发送结果
        public string Status { get; set; }
    }
    public class GetReportResult
    {
        //调用接口返回的结果
        public string Result { get; set; }

        public List<SMSReport> Reports { get; set; }
    }
}